package com.example.lx6_4.order

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.base.db.OrderDao
import com.example.base.entity.OrderEntity
import com.example.base.entity.OrderEntityItem
import com.example.base.entity.Res
import com.example.base.net.ApiService

class OrderSource(val apiService: ApiService,val orderDao: OrderDao,val s:String) : PagingSource<Int,OrderEntityItem>() {
    override fun getRefreshKey(state: PagingState<Int, OrderEntityItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, OrderEntityItem> {
        try {
            var orders : Res<OrderEntity> ?= null
            if(s == "网络"){
                orders = apiService.getOrders()
            }else if(s == "数据库"){
                val order = orderDao.getQueryOrder()
                return LoadResult.Page(order,null,null)
            }
            return LoadResult.Page(orders?.data!!,null,null)
        }catch (e:Exception){
            return LoadResult.Error(e)
        }
    }

}